
from Signals.Kill import Kill
from SistemaOperativo.Planificador import Planificador
from SistemaOperativo.Timer import Timer

class PlanificadorRRConPrioridad(Planificador):
    def __init__(self, quantum):
        self.ready = []
        self.quantum = quantum
        self.timer = Timer(None, self.quantum)
                
    def returnNextPid(self, kernel):
        if(len(self.ready) > 0):
            nextPid = self.ready[0]
            #return self.ready.pop(0)
            for i in self.ready:
                if(kernel.PCBT[nextPid].priority < kernel.PCBT[i].priority):
                    nextPid = i
            self.ready.remove(nextPid)
            return nextPid
        else:
            print("No hay mas elementos en la cola de ready")
            kernel.interrupt(Kill())
            
    def reschedule(self, kernel):
        if len(self.ready)> 0:
            self.timer.set()
            print("readyTamanio", self.ready.__len__())
            super(PlanificadorRRConPrioridad, self).reschedule(kernel)
            if kernel.current_pid is not None:
                self.timer = Timer(kernel, self.quantum)
                self.timer.start()
        else:
            print("No hay mas elementos en la cola de ready")
            
    def getQuantum(self):
        return self.quantum
    
    def terminarProceso(self):
        self.timer._stop()